package cn.wizzer.app.web.commons.ext.websocket.action.msg.group;

import cn.wizzer.app.user.modules.models.enums.MsgStatusEnum;
import cn.wizzer.app.user.modules.services.GroupLogService;
import cn.wizzer.app.user.modules.services.GroupLogUserRelationService;
import cn.wizzer.app.user.modules.services.GroupMemberService;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.framework.base.Result;
import cn.wizzer.framework.page.Pagination;
import cn.wizzer.framework.util.param.Param;
import cn.wizzer.framework.util.param.Param4Map;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

@IocBean
@NettyAction(uri = "/group/history",filters = {TokenFilter.class})
public class HistoryAction extends BaseNettyAction {

    @Inject
    @Reference
    private GroupLogService groupLogService;
    @Inject
    @Reference
    private GroupLogUserRelationService groupLogUserRelationService;
    @Inject
    @Reference
    private GroupMemberService groupMemberService;

    @Override
    public Result action(Channel income, @Param4Map(fields = {@Param(name = "groupId",required = true)}) JSONObject params) {
        String groupId = params.getString("groupId");
        String userId = params.getString("appid");
        Pagination pagination = groupLogService.listPage(params.getInteger("pageNumber"), params.getInteger("pageSize"), groupId, userId);
        //修改未读记录为已读，未读数量变更为0
        groupLogUserRelationService.update(Chain.make("status", MsgStatusEnum.READ),
                Cnd.where("groupId", "=", groupId).and("userId", "=", userId));
        groupMemberService.update(Chain.make("unReadCount", 0),
                Cnd.where("groupId", "=", groupId).and("userId", "=", userId));
        return Result.success().addData(pagination);
    }

}
